package com.joizhang.seckill.controller;

import com.joizhang.seckill.api.service.GoodsService;
import com.joizhang.seckill.api.service.UserService;
import com.joizhang.seckill.api.vo.SecKillGoodsVo;
import com.joizhang.seckill.api.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
@Controller
@RequestMapping("/goods")
public class GoodsController {

    @DubboReference
    private UserService userService;

    @DubboReference
    private GoodsService goodsService;

    /**
     * 商品列表页面
     * QPS:433
     * 1000 * 10
     */
    @RequestMapping(value = "/to_list", produces = "text/html")
    public String getGoodsList(Model model, UserVo userVo) {
        if (Objects.isNull(userVo)) {
            return "redirect:/login/to_login";
        }
        List<SecKillGoodsVo> goodsList = new ArrayList<>();
        List<Long> goodsIds = goodsService.getGoodsIdsFromCache();
        if (!CollectionUtils.isEmpty(goodsIds)) {
            goodsList = new ArrayList<>(goodsIds.size());
            for (Long goodsId : goodsIds) {
                SecKillGoodsVo secKillGoodsVo = goodsService.getGoodsDetailFromCache(goodsId);
                goodsList.add(secKillGoodsVo);
            }
        }
        model.addAttribute("user", userVo);
        model.addAttribute("goodsList", goodsList);
        return "goods_list";
    }

    /**
     * 商品详情页面
     */
    @RequestMapping(value = "/to_detail2/{goodsId}")
    public String goodDetail(Model model, UserVo userVo, @PathVariable("goodsId") Long goodsId) {
        if (Objects.isNull(userVo)) {
            return "redirect:/login/to_login";
        }
        model.addAttribute("user", userVo);
        // 取缓存
        SecKillGoodsVo secKillGoodsVo = goodsService.getGoodsDetailFromCache(goodsId);
        if (Objects.isNull(secKillGoodsVo)) {
            secKillGoodsVo = goodsService.getSecKillGoodsVoByGoodsId(goodsId);
        }
        //根据id查询商品详情
        model.addAttribute("goods", secKillGoodsVo);

        long startTime = secKillGoodsVo.getStartDate().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long endTime = secKillGoodsVo.getEndDate().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        long now = System.currentTimeMillis();

        int secKillStatus = 0;
        int remainSeconds = 0;

        if (now < startTime) {
            //秒杀还没开始，倒计时
            remainSeconds = (int) ((startTime - now) / 1000);
        } else if (now > endTime) {
            //秒杀已经结束
            secKillStatus = 2;
            remainSeconds = -1;
        } else {
            //秒杀进行中
            secKillStatus = 1;
        }
        model.addAttribute("secKillStatus", secKillStatus);
        model.addAttribute("remainSeconds", remainSeconds);
        return "goods_detail";
    }

}
